package com.blb.java08springboot.controller;

import com.blb.java08springboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户控制器
 */
@Controller
public class UserController {

    //用于访问登录页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 处理参数方法1： 定义参数名和表单的name一致
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String handleLogin(String username, String password, Model model,
                              HttpSession session){
        System.out.println(username + "---" + password);
        //模拟账号和密码的验证
        if(!"admin".equals(username) || !"123456".equals(password)){
            //返回数据到页面上
            model.addAttribute("message","账号或密码错误");
            //请求转发
            return "login";
        }
        //登录成功，将用户信息保存到session中
        User user = new User(username,password);
        session.setAttribute("user",user);
        //重定向
        return "redirect:/students";
    }
}
